package com.zx.service.impl;

import com.alibaba.fastjson.JSON;
import com.zx.entity.Product;
import com.zx.mapper.ProductMapper;
import com.zx.service.ProductService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * @author jack.wu
 * @version 1.0
 * @date 2020-03-08
 */
@Slf4j
@Service
public class ProductServiceImpl implements ProductService {

    @Resource
    private ProductMapper productMapper;

    @Override
    public Product findById(Integer pid) {
        log.info("开始根据商品id查询商品信息，商品id{}", pid);
        Product product = productMapper.findById(pid).get();
        log.info("查询商品信息成功，内容未{}", JSON.toJSONString(product));
        return product;
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public void reduceInventory(Integer pid, int num) {
        Product product = productMapper.findById(pid).get();
        product.setStock(product.getStock()-num);
        // 模拟异常
        int i = 1 / 0;
        productMapper.save(product);
    }
}
